var mysql = require('mysql');

var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'zhixin',
    timezone: "08:00"
})

function doSql(sql, params = []) {
    return new Promise((resolve, reject) => {

        pool.getConnection(function (err, connection) {
            if (connection === undefined) {
                resolve(({ code: 10000, message: '登录超时' }))
                return
            }
            if (err) {
                reject(err)
            } else {
                connection.query(sql, params, (error, resultes, fileds) => {
                    connection.release();
                    resolve({ error, resultes, fileds })
                })
            }
        })

    })
}
function fool() {
    return {
        'success': 'true',
        'errorCode': 401,
        'msg': '当前没有数据',
    }
}

function hand(data) {
    return {
        success: true,
        'code': 20000,
        errorCode: 0,
        msg: '数据请求成功',
        data: data,
    }
}

function heal(data, total) {
    return {
        'success': 'true',
        'code': 20000,
        'errorCode': 0,
        'msg': '数据请求成功',
        'data': data,
        total: total,
    }
}

exports.doSql = doSql;
exports.heal = heal;
exports.hand = hand;
exports.fool = fool;